package com.hrw.android.player.dao.impl;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.ContextWrapper;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import com.hrw.android.player.dao.AudioDao;
import com.hrw.android.player.db.constants.UriConstant;

public class AudioDaoImpl extends ContextWrapper implements AudioDao {
	public AudioDaoImpl(Context base) {
		super(base);
	}

	@Override
	public String getMusicPathByName(String name) {
		String path = null;
		ContentResolver cr = getContentResolver();
		Uri uri = Uri.parse(UriConstant.AUDIO_LIST_URI);
		String[] projection = { "audio_path" };
		String selection = "audio_name = ?";
		String[] selectionArgs = { name };
		Cursor c = cr.query(uri, projection, selection, selectionArgs, null);
		if (c.moveToFirst()) {
			path = c.getString(0);
		}
		return path;
	}

	@Override
	public List<String> getMusicListByPId(String id) {
		List<String> musicList = new ArrayList<String>();
		ContentResolver cr = getContentResolver();
		Uri uri = Uri.parse(UriConstant.AUDIO_LIST_URI);
		String[] projection = { "audio_name" };
		String selection = "playlist_id = ?";
		String[] selectionArgs = { id };
		Cursor c = cr.query(uri, projection, selection, selectionArgs, null);
		if (c.moveToFirst()) {
			for (int i = 0; i < c.getCount(); i++) {
				c.moveToPosition(i);
				musicList.add(c.getString(0));
			}
		}
		c.close();
		return musicList;

	}

	@Override
	public void addMediaToPlaylist(ContentValues values) {
		Uri uri = Uri.parse(UriConstant.AUDIO_LIST_URI);
		ContentResolver cr = getContentResolver();
		cr.insert(uri, values);
	}

	@Override
	public void removeAudioById(String id) {

	}
	@Override
	public List<String> getLocalAudioList() {
		List<String> musicList = new ArrayList<String>();
		ContentResolver resolver = getContentResolver();
		Cursor cursor = resolver.query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
				MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
		if (cursor.moveToFirst()) {
			for (int i = 0; i < cursor.getCount(); i++) {
				cursor.moveToPosition(i);
				musicList.add(cursor.getString(2));
			}
		}
		cursor.close();
		return musicList;
	}
}
